package stripesbook.action;

import org.stripesbook.rayures.view.ViewObject;
import org.stripesbook.rayures.view.stripes.SErrors;
import org.stripesbook.rayures.view.stripes.SForm;
import org.stripesbook.rayures.view.stripes.SLink;
import org.stripesbook.rayures.view.stripes.SOption;
import org.stripesbook.rayures.view.stripes.SPassword;
import org.stripesbook.rayures.view.stripes.SSelect;
import org.stripesbook.rayures.view.stripes.SSubmit;
import org.stripesbook.rayures.view.stripes.SText;

public class RegisterView extends ViewObject<RegisterView> {
  private RegisterActionBean actionBean;

  public RegisterView actionBean(RegisterActionBean actionBean) {
    this.actionBean = actionBean;
    return getThis();
  }

  public SErrors getErrors() {
    return tag(SErrors.class).globalErrorsOnly(true);
  }

  public SForm getRegisterForm() {
    return tag(SForm.class).beanclass(RegisterActionBean.class)
      .addInputs(
        tag(SText.class).name("user.firstName"),
        tag(SText.class).name("user.lastName"),
        tag(SText.class).name("user.username"),
        tag(SPassword.class).name("user.password"),
        tag(SPassword.class).name("confirmPassword"),
        getSelectNumberOfAliases())
      .addButtons(
        tag(SSubmit.class).name("register"),
        tag(SSubmit.class).name("cancel"));
  }

  public SLink getLoginLink() {
    return tag(SLink.class).beanclass(LoginActionBean.class)
      .label(bundle.get("gotoLogin"));
  }

  private SSelect getSelectNumberOfAliases() {
    String howManyAliases = bundle.get("howManyAliases");
    SSelect select = tag(SSelect.class).name("numberOfAliases")
      .add(tag(SOption.class).value("").label(howManyAliases));

    for (int i = actionBean.getMinAliases(); i <= actionBean.getMaxAliases(); i++) {
      select.add(tag(SOption.class).value(i).label(String.valueOf(i)));
    }
    return select;
  }
}
